﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Text;

using ZoneFiveSoftware.Common.Data.Fitness;


namespace SportTracksSwimSetPlugin.Common
{
    public static class SwimSetSettings
    {

        private class xmlTags
        {
            public const string rootElement = "swimSetPluginSettings";
            public const string perHundredPaceSeconds = "perHundredPaceSeconds";
            public const string updateAtImport = "updateAtImport";
            public const string categoryList = "category";

        }

        public static void Load(XmlElement pluginNode)
        {
            try
            {
                string attr;

                perHundredPaceSeconds =
                    XmlConvert.ToDouble(pluginNode.GetAttribute(xmlTags.perHundredPaceSeconds));
                updateAtImport = 
                    XmlConvert.ToBoolean(pluginNode.GetAttribute(xmlTags.updateAtImport));
                attr = pluginNode.GetAttribute(xmlTags.categoryList);


                if (categoryList == null && attr.Split(':').Length > 0)
                {
                    categoryList = new List<string> { };
                }

                Log.WriteToLogfile("Reading Categories from list:\n\r" + attr, Log.LogLevel.DEBUG);
                foreach( string category in attr.Split(':'))
                {
                    Log.WriteToLogfile("\t" + category, Log.LogLevel.DEBUG);
                    categoryList.Add(category);
                }
                
            }
            catch (System.Exception e)
            {
                Common.Log.WriteToLogfile("Failed to load Settings\n\r" +e.Message +"\n\r" +e.StackTrace, Common.Log.LogLevel.ERROR);
            }
        }

        public static void Save(XmlElement pluginNode)
        {
            try
            {
                string categoryListStr = "";

                pluginNode.SetAttribute(xmlTags.perHundredPaceSeconds,
                    XmlConvert.ToString(perHundredPaceSeconds));
                pluginNode.SetAttribute(xmlTags.updateAtImport,
                    XmlConvert.ToString(updateAtImport));

                for (int i = 0; i < categoryList.Count; i++)
                {
                    if (i == 0) 
                        categoryListStr += categoryList[i];
                    else 
                        categoryListStr += ':' + categoryList[i];
                }
                Log.WriteToLogfile("Adding categoryListStr:\n\r" + categoryListStr, Log.LogLevel.DEBUG);

                pluginNode.SetAttribute(xmlTags.categoryList, categoryListStr);

            }
            catch (System.Exception e)
            {
                Log.WriteToLogfile("Failed to save Settings\n\r" + e.Message + "\n\r" + e.StackTrace, Log.LogLevel.ERROR);
            }
        }

        public static double PerHundredPaceSeconds
        {
            get { return perHundredPaceSeconds; }
            set { perHundredPaceSeconds = value; }
        }

        public static bool UpdateAtImport
        {
            get { return updateAtImport; }
            set { updateAtImport = value; }
        }

        public static List<string> CategoryList
        {
            get { return categoryList; }
            set { categoryList = value; }
        }

        public static bool UseTotalDuration
        {
            get { return useTotalDuration; }
            set { useTotalDuration = value; }
        }

        private static double perHundredPaceSeconds = 0;
        private static bool updateAtImport = false;
        private static bool useTotalDuration = true; 
        private static List<string> categoryList = null;

    }

}